package com.yoo.ons.api

import com.framework.network.annotations.getRetrofitMethod
import com.yoo.module_base.utils.SaveDeviceUtil
import okhttp3.Request
import org.json.JSONObject


/**
 * @ClassName: CommonParams
 * @Description: 公共参数
 * @Author: You
 */
object NetworkParams {
    /**
     * 公参
     */
    fun getCommonParams(): MutableMap<String, Any> {
        val commonParams = mutableMapOf<String, Any>()
        return commonParams
    }

    /**
     * 头部公参数
     */
    fun getHeadCommonParams(): MutableMap<String, Any> {
        val commonParams = mutableMapOf<String, Any>()
        return commonParams
    }

    /**
     * 添加头部参数
     */
    fun getMapWithHeaders(request: Request): HashMap<String, Any> {
        val headMap = HashMap<String, Any>()
        val jsonObject = JSONObject()
        jsonObject.put("timestamp", System.currentTimeMillis().toString())
        jsonObject.put("channel", "l1")
        jsonObject.put("appCode", "ons")
        jsonObject.put("applicationID", com.yoo.ons.BuildConfig.APPLICATION_ID)
        if (SaveDeviceUtil.getAppsFlyerUID() != null) {
            jsonObject.put("appsFlyerUID", SaveDeviceUtil.getAppsFlyerUID())
        }
        if (SaveDeviceUtil.getToken() != null) {
            jsonObject.put("token", SaveDeviceUtil.getToken()!!)
        }
        jsonObject.put("appSource", "1")
        jsonObject.put("appVersion", com.yoo.ons.BuildConfig.VERSION_CODE)
        jsonObject.put("appVersionName", com.yoo.ons.BuildConfig.VERSION_NAME)
        headMap["basicParams"] = addHeadNewParams(request, jsonObject).toString()
        return headMap
    }

    private fun addHeadNewParams(request: Request, jsonObject: JSONObject): JSONObject {
        getRetrofitMethod(request)?.let {
            val map = HeadDynamicsParamsHelper.headDynamicsParamMap.get(it.name)
            if (map != null && map.size > 0) {
                map.forEach { t, u -> jsonObject.put(t, u) }
            }
            return jsonObject
        }

        return jsonObject
    }
}